<?php
include '../lib/BLL/DonViBLL.php';
include '../lib/BLL/DmMonHocBLL.php';
include '../lib/BLL/DonViHocVuBLL.php';
include '../lib/BLL/SinhVienBLL.php';
include '../lib/BLL/DmCaHocBLL.php';
include '../lib/BLL/HoiDongThiBLL.php';

$madsthi = $_GET['dsthi'];


$hoidong = new HoiDongThiBLL();
//$hd = $hoidong->getHoiDong_indirect($ma_donvi, $hocvu);
//$smarty->assign('hd',$hd);

$dv = new DonViBLL();
$mh = new DmMonHocBLL();
$dvhv = new DonViHocVuBLL();
$sv = new SinhVienBLL();
$cathi = new DmCaHocBLL();
$dsthi = $db->getOneRow("SELECT dst.ma_hoidongthi, dst.ma_mon_ctdt, tt.ten_donvi, dmn.ten_nganh, mh.madm_mh, tt.ma_donvi, mh.ten_mh, dvhv.ma_donvihocvu, dst.ngaythi, dst.madm_ca, dst.trangthai_dsthi FROM tbl_dsthi AS dst
INNER JOIN tbl_hoidongthi AS hdt ON hdt.ma_hoidong = dst.ma_hoidongthi
INNER JOIN tbl_donvi AS tt ON tt.ma_donvi = hdt.ma_donvi
INNER JOIN tbl_donvihocvu AS dvhv ON hdt.ma_donvihocvu = dvhv.ma_donvihocvu
INNER JOIN tbl_mon_ctdt AS mctdt ON mctdt.ma_mon_ctdt = dst.ma_mon_ctdt
INNER JOIN tbl_ctdt AS ctdt ON ctdt.ma_ctdt = mctdt.ma_ctdt
INNER JOIN dm_nganh AS dmn ON dmn.madm_nganh = ctdt.madm_nganh
INNER JOIN dm_monhoc AS mh ON mh.madm_mh = mctdt.ma_monhoc
WHERE dst.ma_dsthi = '$madsthi'");

$donvi = $dsthi['ten_donvi'];
$madonvi = $dsthi['ma_donvi'];
$smarty->assign('madv', $madonvi);
$monhoc = $dsthi['ten_mh'];
$mamonhoc = $dsthi['madm_mh'];
$smarty->assign('mamh', $mamonhoc);
$hocvu = $dsthi['ma_donvihocvu'];

$sods = $dsthi['trangthai_dsthi'];
$mahdthi = $dsthi['ma_hoidongthi'];
$macathi = $dsthi['madm_ca'];
$ngaythi = $dsthi['ngaythi'];
$nganh = $dsthi['ten_nganh'];
$ma_mon_ctdt = $dsthi['ma_mon_ctdt'];
/*
$listdkm = $db->getList("SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi = '$madsthi'");
$dkmarr = array();
foreach($listdkm as $dkm){
	$dkmarr[] = $dkm['ma_dkm'];
}
$dkmarr = implode(",", $dkmarr);
if(empty($dkmarr)) $dkmarr = 0;
 /*AND dkm.ma_dkm NOT IN(
SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi =  '113AAB10801')
SELECT sv.hoten_sv FROM tbl_sinhvien AS sv INNER JOIN tbl_dangkymon AS dkm ON dkm.ma_sv = sv.ma_sv
WHERE dkm.ma_dkm IN(SELECT CONCAT(ma_dkm) FROM tbl_diemthi WHERE ma_dsthi = '113AAB10801')*/
//Get List SV
$sql0 = "SELECT lm.ngaybatdau FROM tbl_lopmon AS lm
INNER JOIN tbl_lopmon_monctdt AS lmctdt ON lm.ma_lopmon = lmctdt.ma_lopmon
INNER JOIN  tbl_mon_ctdt AS mctdt ON lmctdt.ma_mon_ctdt = mctdt.ma_mon_ctdt
INNER JOIN dm_monhoc AS mh ON mctdt.ma_monhoc = mh.madm_mh
WHERE lm.ma_donvi = '$madonvi' AND lm.ma_donvihocvu = '$hocvu'
AND mh.madm_mh = '$mamonhoc'";
$ngaylm = $db->getList($sql0);

$sql = "SELECT dkm.ma_sv, dkm.ma_dkm, sv.hoten_sv, sv.ngaysinh_sv, dkm.ma_lopmon FROM tbl_dangkymon AS dkm
INNER JOIN tbl_sinhvien AS sv ON sv.ma_sv = dkm.ma_sv
WHERE ma_lopmon IN(SELECT lm.ma_lopmon FROM tbl_lopmon AS lm
INNER JOIN tbl_lopmon_monctdt AS lmctdt ON lm.ma_lopmon = lmctdt.ma_lopmon
INNER JOIN  tbl_mon_ctdt AS mctdt ON lmctdt.ma_mon_ctdt = mctdt.ma_mon_ctdt
INNER JOIN dm_monhoc AS mh ON mctdt.ma_monhoc = mh.madm_mh
WHERE lm.ma_donvi = '$madonvi' AND lm.ma_donvihocvu = '$hocvu'
AND mh.madm_mh = '$mamonhoc') AND dkm.ma_dkm NOT IN(SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi IN(SELECT DISTINCT ma_dsthi FROM `tbl_dsthi` WHERE ma_hoidongthi = '$mahdthi' AND ma_mon_ctdt = '$ma_mon_ctdt') ) ORDER BY dkm.ma_lopmon ASC, sv.ten_sv ASC, sv.hoten_sv";
$svarr = $db->getList($sql);
$slsv1 = sizeof($svarr);


$svarr2 = $db->getList("SELECT dkm.ma_sv, dkm.ma_dkm, sv.hoten_sv, sv.ngaysinh_sv, dkm.ma_lopmon FROM tbl_dangkymon AS dkm
INNER JOIN tbl_sinhvien AS sv ON sv.ma_sv = dkm.ma_sv
WHERE dkm.ma_dkm IN(SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi = '$madsthi')  ORDER BY sv.ten_sv ASC, sv.hoten_sv");

$ds_sv = $sv->getListSinhvien();
$slsv2 = sizeof($svarr2);

$ds_cathi = $cathi->getListDmCaHoc();

$smarty->assign('monhoc',$monhoc);
$smarty->assign('donvi',$donvi);
$smarty->assign('hocvu',$hocvu);
$smarty->assign('hdthi',$mahdthi);
$smarty->assign('sods',$sods);
$smarty->assign('madsthi',$madsthi);
$smarty->assign('nganh', $nganh);
$smarty->assign('macathi', $macathi);
$smarty->assign('ds_sv',$ds_sv);
$smarty->assign('ds_cathi',$ds_cathi);
$smarty->assign('date',$ngaythi);
$smarty->assign('sinhvien',$svarr);
$smarty->assign('cursinhvien', $svarr2);
$smarty->assign('slsv1', $slsv1);
$smarty->assign('slsv2', $slsv2);
$smarty->assign('ngaylm', $ngaylm);
$smarty->assign('mctdt',$ma_mon_ctdt);
$smarty->display('tochucthi/suadsthi.tpl');

?>